package com.banmoon.algorithm.leetcode;

public class Question2104 {

    public static void main(String[] args) {
        int[] nums = {4, -2, -3, 4, 1};
//        int[] nums = {1, 2, 3};
        System.out.println(subArrayRanges(nums));
    }

    public static long subArrayRanges(int[] nums) {
        long sum = 0;
        for (int i = 0; i < nums.length-1; i++) {
            int minIndex = i;
            int maxIndex = i;
            for (int j = i+1; j < nums.length; j++) {
                if(nums[j]>nums[maxIndex])
                    maxIndex = j;
                else if(nums[j]<nums[minIndex])
                    minIndex = j;
                sum += nums[maxIndex] - nums[minIndex];
            }
        }
        return sum;
    }

}
